package com.tpv.app.aidlclient;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.tpv.app.aidldemo.IRemoteInterface;

public class MainActivity extends Activity
{
    
    private IRemoteInterface mAidlBinder;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView btn = findViewById(R.id.btn_remote_service);
        final TextView btnAdd = findViewById(R.id.btn_add);
        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(MainActivity.this,"点击事件",Toast.LENGTH_SHORT).show();
                if (null!=mAidlBinder)
                {
                    Log.e(getClass().getSimpleName(),"aidl测试");
                    try
                    {
                        String text = mAidlBinder.callRemote();
                        if (!TextUtils.isEmpty(text))
                        {
                            btn.setText(text);
                        }
                    } catch (RemoteException e)
                    {
                        e.printStackTrace();
                    }
                }else {
                    Log.e(getClass().getSimpleName(),"mAidlBinder is null");
    
                }
            }
        });
        btnAdd.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (null!=mAidlBinder)
                {
                    Log.e(getClass().getSimpleName(),"aidl测试");
                    try
                    {
                        int add = mAidlBinder.add(10, 9);
                        btnAdd.setText(""+add);
                    } catch (RemoteException e)
                    {
                        e.printStackTrace();
                    }
                }else {
                    Log.e(getClass().getSimpleName(),"mAidlBinder is null");
        
                }
            }
        });
        Intent intent=new Intent();
        intent.setAction("com.tpv.app.aidldemo.remote_service");
        intent.setPackage("com.tpv.app.aidldemo");
        bindService(intent,aidl,BIND_AUTO_CREATE);
    }
    
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        unbindService(aidl);
    }
    
    private ServiceConnection aidl =new ServiceConnection()
    {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service)
        {
            Log.e(getClass().getSimpleName(),"绑定服务:");
            mAidlBinder = IRemoteInterface.Stub.asInterface(service);
        }
    
        @Override
        public void onServiceDisconnected(ComponentName name)
        {
            mAidlBinder=null;
        }
    };
}
